package com.mjk.common.core.jdbc.service.plus.provider.impl.example;


import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.base.model.BaseExample;
import com.mjk.common.core.jdbc.util.TableUtils;
import com.mjk.common.util.MapUtils;
import com.mjk.common.util.StrUtils;

import java.util.List;
import java.util.Map;

/**
 * Sql语句提供类 用于解析example
 */
public class ExampleSQLProvider<T extends BaseEntity, K extends BaseExample> {

    public String excuteSql(Map<String, Object> map) {
        return MapUtils.getString(map, "sql");
    }

    public String countByExample(Map<String, Object> map) {
        BaseExample example = (BaseExample) map.get("example");
        return TableUtils.getHandler(example).countByExample(map);
    }

    public String deleteByExample(Map<String, Object> map) {
        BaseExample example = (BaseExample) map.get("example");
        return TableUtils.getHandler(example).deleteByExample(map);
    }

    public String deleteByPrimaryKey(T t) {
        return TableUtils.getHandler(t).deleteByPrimaryKey(t);
    }

    public String insert(Map<String, Object> map) {
        BaseEntity app = (BaseEntity) map.get("item");
        return TableUtils.getHandler(app).insert(map);
    }

    public String selectByExample(Map<String, Object> map) {
        BaseExample example = (BaseExample) map.get("example");
        return TableUtils.getHandler(example).selectByExample(map);
    }

    public String selectByPrimaryKey(Map<String, Object> map) {
        BaseEntity app = (BaseEntity) map.get("dto");
        return TableUtils.getHandler(app).selectByPrimaryKey(map);
    }

    public String updateByPrimaryKeySelective(Map<String, Object> map) {
        BaseEntity t = (BaseEntity) map.get("dto");
        return TableUtils.getHandler(t).updateByPrimaryKeySelective(map);
    }

    public String updateByExampleSelective(Map<String, Object> map) {
        BaseEntity app = (BaseEntity) map.get("dto");
        return TableUtils.getHandler(app).updateByExampleSelective(map);
    }

    @SuppressWarnings({"rawtypes" })
    public String deleteBaseList(Map<String, Object> map) {
        List list = (List) map.get("list");
        BaseEntity entity = (BaseEntity) list.get(0);
        return TableUtils.getHandler(entity).deleteBaseList(map);
    }

    @SuppressWarnings("rawtypes")
    public String updateBaseList(Map<String, Object> para) {
        List list = (List) para.get("list");
        BaseEntity entity = (BaseEntity) list.get(0);
        return TableUtils.getHandler(entity).updateBaseList(para);
    }

    @SuppressWarnings("rawtypes")
    public String insertBaseList(Map<String, Object> para) {
        List list = (List) para.get("list");
        StrUtils.notNull(list, "list is null");
        BaseEntity entity = (BaseEntity) list.get(0);
        return TableUtils.getHandler(entity).insertBaseList(para);
    }
}
